package com.example.demo.controller;

import com.alibaba.druid.sql.visitor.ExportParameterVisitorUtils;
import com.example.demo.pojo.Course;
import com.example.demo.pojo.User;
import com.example.demo.service.StudentService;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import java.rmi.server.ExportException;
import java.util.Date;
import java.util.List;

@Controller
@Transactional
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private UserService userService;
    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Value("${spring.mail.username}")
    private String sender;
    @ApiOperation("跳转邮箱发送")
    @RequestMapping("/toEmail")
    public String toEmail(){
        return "admin/email";
    }
    @ApiOperation("邮箱发送信息")
    @RequestMapping("/email")
    public String email(@RequestParam("topic") String topic,
                        @RequestParam("context") String context,
                        @RequestParam("email") String email
        ){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject(topic);
        message.setText(context);
        message.setTo(email);
        message.setFrom(sender);
        javaMailSender.send(message);
        return "admin/admin";
    }
    @ApiOperation("邮箱发送信息")
    @RequestMapping("/email1")
    public String email1(@RequestParam("topic") String topic,
                         @RequestParam("context") String context,
                         @RequestPart("face") MultipartFile face,
                         @RequestParam("email") String email) throws MessagingException {
        String originalFilename=face.getOriginalFilename();
        MimeMessage message=javaMailSender.createMimeMessage();
        //对message进行组装
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setSubject(topic);
        helper.setText(context);
        helper.addAttachment(originalFilename,face);
        helper.setSentDate(new Date());
        helper.setFrom(sender);
        helper.setTo(email);
        javaMailSender.send(message);
        return "admin/admin";
    }
    @ApiOperation(value = "获取所有学生")
    @RequestMapping("/getAllUser")
    public String getAllUser(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer size, Model model){
        List<User> list=userService.getAllUser(pageNum,size);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/mangeUser";
    }
    @ApiOperation("删除用户")
    @RequestMapping("/deleteUser")
    public String deleteUser(@RequestParam("uid") String uid){
        userService.deleteUser(uid);
        studentService.deleteUser(uid);
        return "redirect:/admin/getAllUser";
    }
    @ApiOperation("Exl导出用户数据")
    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws  Exception{
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("用户信息表");

      /*  List<Teacher> classmateList = teacherservice.teacherinfor();*/
        List<User> list=userService.getAllUser();

        String fileName = "userinf"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "识别号", "账号", "密码", "角色"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (User user : list) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(user.getUid());
            row1.createCell(1).setCellValue(user.getUaccount());
            row1.createCell(2).setCellValue(user.getUpassword());
            row1.createCell(3).setCellValue(user.getUrole());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
